package com.biyu.monitormanage.controller;

import com.biyu.monitormanage.common.annotation.SysLog;
import com.biyu.monitormanage.common.feign.AdminFeignService;
import com.biyu.monitormanage.common.utils.BaseResponse;
import com.biyu.monitormanage.common.utils.PageUtils;
import com.biyu.monitormanage.entity.MachineEntity;
import com.biyu.monitormanage.model.bo.MachineMonitorBo;
import com.biyu.monitormanage.model.dto.MonitorMapDto;
import com.biyu.monitormanage.service.MachineService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
/**
 * 机器表
 *
 * @author 15491
 * @email byb15491665510220@163.com
 * @date 2022-03-12 11:39:22
 */
@RestController
@RequestMapping("monitormanage/machine")
public class MachineController {

    private final MachineService machineService;

    @Autowired
    public MachineController(MachineService machineService) {
        this.machineService = machineService;
    }

    /**
     * 被检测机器数据显示 —— 数据监控
     * @param params map
     * @return response
     */
    @GetMapping("/monitor/apply")
    @RequiresPermissions("monitormanage:machine:monitorApply")
    public BaseResponse monitorApply(@RequestParam Map<String, Object> params) {
        List<MachineMonitorBo> monitoringParamDtos = machineService.queryMonitorAndApply(params);
        return BaseResponse.ok().put("data", monitoringParamDtos);
    }

    @GetMapping("/monitor/map")
    @RequiresPermissions("monitormanage:machine:monitorMap")
    public BaseResponse monitorMap(@RequestParam Map<String, Object> params) {
        List<MonitorMapDto> monitorMapDto = machineService.monitorMap(params);
        return BaseResponse.ok().put("data", monitorMapDto);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("monitormanage:machine:list")
    public BaseResponse list(@RequestParam Map<String, Object> params){

        PageUtils page = machineService.queryPage(params);

        return BaseResponse.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping(value = "/info/{id}", method = RequestMethod.GET)
    @RequiresPermissions("monitormanage:machine:info")
    public BaseResponse info(@PathVariable("id") Long id){
		MachineEntity machine = machineService.getById(id);

        return BaseResponse.ok().put("machine", machine);
    }

    /**
     * 保存
     */
    @SysLog("新增服务器")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @RequiresPermissions("monitormanage:machine:save")
    public BaseResponse save(@RequestBody MachineEntity machine){
		machineService.save(machine);

        return BaseResponse.ok();
    }

    /**
     * 修改
     */
    @SysLog("修改服务器信息")
    @RequestMapping("/update")
    @RequiresPermissions("monitormanage:machine:update")
    public BaseResponse update(@RequestBody MachineEntity machine){
		machineService.updateById(machine);
        return BaseResponse.ok();
    }

    /**
     * 删除
     */
    @SysLog("删除服务器信息")
    @RequestMapping("/delete")
    @RequiresPermissions("monitormanage:machine:delete")
    public BaseResponse delete(@RequestBody Long[] ids){
		machineService.removeByIds(Arrays.asList(ids));

        return BaseResponse.ok();
    }

}
